package com.parp;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import com.utils.Circle;
import com.utils.SoundLine;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

public class Visuals extends View {

	private final List<Circle> circles =  new ArrayList<Circle>();
	private final List<Circle> oldCircles =  new ArrayList<Circle>();
	private final List<SoundLine> lines =  new ArrayList<SoundLine>();
	private final List<SoundLine> oldLines =  new ArrayList<SoundLine>();
	private final Random randomGen = new Random(System.currentTimeMillis());
	private byte[] Bytes = null;
	
	private final Paint circlePaint = new Paint();
	private final Paint linePaint = new Paint();
	private final Paint lineEndPaint = new Paint();
	
	
	private int lastLine1 = -1;
	private int lastLine2 = -1;
	
	private Context context;
	private int scale = 50;
	public Visuals(Context context) {
		super(context);
		this.context = context;
		
		circlePaint.setStrokeWidth(3);
		circlePaint.setColor(0xFF663300);		
		
		linePaint.setStrokeWidth(1);
		linePaint.setColor(0x88552706);
		
		lineEndPaint.setStrokeWidth(1);
		lineEndPaint.setColor(0xFFAA5511);
		// TODO Auto-generated constructor stub
	}
	public Visuals(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context = context;
		
		circlePaint.setStrokeWidth(3);
		circlePaint.setColor(0xFF663300);		
		
		linePaint.setStrokeWidth(1);
		linePaint.setColor(0x88552706);
		
		lineEndPaint.setStrokeWidth(1);
		lineEndPaint.setColor(0xFFAA5511);
		// TODO Auto-generated constructor stub
	}
	public Visuals(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		this.context = context;
		circlePaint.setStrokeWidth(3);
		circlePaint.setColor(0xFF663300);		
		
		linePaint.setStrokeWidth(1);
		linePaint.setColor(0x88552706);
		
		lineEndPaint.setStrokeWidth(1);
		lineEndPaint.setColor(0xFFAA5511);
		// TODO Auto-generated constructor stub
	}
	
	public void newCircle(int x, int y)
	{
		synchronized (circles) 
		{
			if (circles.size() < 30)
			{
				Log.i("PARP","Adding circle " + circles.size());
				x = x + (20 - nextRandInt(40));
				y = y + (20 - nextRandInt(40));
				int r = 5 + nextRandInt(20);
				circles.add(new Circle(x,y,r));
			}
		}
	}
	
	public void newLines(byte[] lengths)
	{
		//lines.add(new Line(x,length));
		Bytes = lengths;
	}
	
	@Override
	protected void onDraw(Canvas canvas) {
		int i = 1;
		if(Bytes != null)
		{
			for(Byte b : Bytes)
			{
				canvas.drawLine(canvas.getWidth()/2 - b, i, canvas.getWidth()/2 + b, i, linePaint);
				if(lastLine1 > -1)
				{
					//canvas.drawLine(canvas.getWidth()/2 - b , i, lastLine1, i-1, lineEndPaint);
					//canvas.drawLine(canvas.getWidth()/2 + b , i, lastLine2, i-1, lineEndPaint);
					//canvas.drawPoint(canvas.getWidth()/2 - b, i, lineEndPaint);
				}
				lastLine1 = canvas.getWidth()/2 - b;
				lastLine2 = canvas.getWidth()/2 + b;
				i++;
			}
			lastLine1 = -1;
			lastLine2 = -1;
		}
		synchronized (circles)
		{
			for(Circle c : circles)
			{
				circlePaint.setARGB(255, nextRandInt(255), nextRandInt(255), nextRandInt(255));
				canvas.drawCircle(c.getX(), c.getY(), c.getRadius(), circlePaint);
				c.setRadius(c.getRadius() - 1);
				Log.i("PARP","Drawing circle at " + c.getX() + ", " + c.getY());
				if (c.getRadius() < 1)
				{
					oldCircles.add(c);
					Log.i("PARP","Removing a circle");
				}
			}
			if(oldCircles.size() > 0)
			{
				circles.removeAll(oldCircles);
				oldCircles.clear();
			}
		}		
		if(circles.size() == 0 && lines.size() == 0)
		{
			invalidate();
			//super.onDraw(canvas);
		}
		else
		{
			invalidate();
			Log.i("PARP","Visualiser re-drawing");
			super.onDraw(canvas);
		}
		
	}

	private int nextRandInt(int range)
	{
		return randomGen.nextInt(range);
	}
	
}
